package com.smrs.deliver.webapp.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.smrs.deliver.enums.DeliverStatusEnum;
import com.smrs.deliver.model.DeliverHeaderModel;
import com.smrs.deliver.model.DeliverHeaderSearchModel;
import com.smrs.enums.ActionCommandEnum;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;




public class DeliverHeaderJsonAction extends DeliverHeaderAction{
	private static final long serialVersionUID = 1L;
	
	public String searchDeliverHeaderJson(){
		/*DeliverHeaderSearchModel deliverHeaderSearchModel = new DeliverHeaderSearchModel();
		deliverHeaderSearchModel.setDeliverHeader(deliverHeader);		
		deliverHeaderSearchModel.setPager(pager);		
		pager=deliverHeaderService.getByNameLikePager(deliverHeaderSearchModel);
		*/
		//prepareList();
		//DeliverHeaderSearchModel deliverHeaderSearchModel = new DeliverHeaderSearchModel();
		if(searchModel==null){
			searchModel = new DeliverHeaderSearchModel();
		}
		ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);
		if(action!=null && searchModel.getStatus()==null){
			switch(action){
				case audit:
				case unaudit:{					
					searchModel.setStatus(DeliverStatusEnum.FRESH.getId());					
					break;
				}
				case account:
				case unaccount:{
					searchModel.setStatus(DeliverStatusEnum.AUDITED.getId());
					break;
				}
				default:
					searchModel.setStatus(DeliverStatusEnum.FRESH.getId());
			}
		}
		pager=deliverHeaderService.findDeliverHeader(searchModel,pager);
		//this.actionCommand="query";
		return SUCCESS;
	}
	
	public String updateDeliverHeaderJson(){
		if(StringUtils.isEmpty(this.actionCommand)){
			//deliverHeader = deliverHeaderService.getByPK(deliverHeader.getId());	
			//store = this.storeService.getByPK(deliverHeader.getStoreId());
			//region = regionService.getByPK(deliverHeader.getRegion().getId());
			this.addActionError("沒有输入操作方向,请输入操作方向");	
			return SUCCESS;
		}
		ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);
		switch(action){
			case audit:{
				List<Long>  listIds = parseDeliverHeaderIds(deliverHeaderIds);
				for(Long deliverId:listIds){
					DeliverHeaderModel model = deliverHeaderService.getByPK(deliverId);
					model.setStatus(DeliverStatusEnum.AUDITED.getId());
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAuditor(userModel.getName());
					model.setAuditTime(new Date());
					deliverHeaderService.updateModel(model);				
				}			
				this.addActionMessage("修改成功");	
				break;
			}
			case unaudit:{
				List<Long>  listIds = parseDeliverHeaderIds(deliverHeaderIds);
				for(Long deliverId:listIds){
					DeliverHeaderModel model = deliverHeaderService.getByPK(deliverId);
					model.setStatus(DeliverStatusEnum.FRESH.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAuditor(userModel.getName());
					model.setAuditTime(new Date());
					deliverHeaderService.updateModel(model);
				}			
				this.addActionMessage("修改成功");	
				break;
				}
			case account:{
				prepareAccountStatus();
				List<Long>  listIds = parseDeliverHeaderIds(deliverHeaderIds);
				for(Long deliverId:listIds){
					DeliverHeaderModel model = deliverHeaderService.getByPK(deliverId);
					model.setStatus(DeliverStatusEnum.ACCOUNTED.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAccountant(userModel.getName());
					model.setAccoutTime(new Date());
					deliverHeaderService.updateModel(model);
					}			
				this.addActionMessage("修改成功");	
				break;
				}
			case unaccount:{
				prepareAccountStatus();
				List<Long>  listIds = parseDeliverHeaderIds(deliverHeaderIds);
				for(Long deliverId:listIds){
					DeliverHeaderModel model = deliverHeaderService.getByPK(deliverId);
					model.setStatus(DeliverStatusEnum.AUDITED.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAccountant(userModel.getName());
					model.setAccoutTime(new Date());
					deliverHeaderService.updateModel(model);
					}			
				this.addActionMessage("修改成功");	
				break;
				}
			default:{
				this.addActionError("沒有输入操作方向,请输入操作方向");	
				}
		}
		return SUCCESS;
	}
	
}
